<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('user.bind.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm">
          <el-form-item :label="i18n('user.username')" prop="username">
            <span>{{ dataForm.username }}</span>
          </el-form-item>

          <el-form-item :label="i18n('user.nickname')" prop="nickname">
            <span>{{ dataForm.nickname }}</span>
          </el-form-item>

          <el-form-item :label="i18n('user.qrcode_url')">
            <el-image v-if="dataForm.is_bind == 0" class="image" :src="dataForm.qrcode_url">
              <template #error>
                <div class="image-slot">
                  <el-icon><Picture /></el-icon>
                </div>
              </template>
            </el-image>
            <span v-else>已绑定</span>
          </el-form-item>

          <el-form-item :label="i18n('user.bind.cancel_bind')" v-if="dataForm.is_bind == 1">
            <el-button size="small" type="danger"  @click="handleCancelBind(dataForm.id)">
              {{ i18n('common.cancel') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="user-bind-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useSocket } from '@/utils/websocket'

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('user/bind')

  const rolelist = ref([])
  const clientList = ref([])

  const dataForm = reactive({
    id: 0,
    username: '',
    nickname: '',
    qrcode_url: '',
    is_bind: 0,
  })

  const init = () => {
    dataForm.id = query.id || 0

    if (dataForm.id) {
      $http({
        url: $http.adornUrl(`/${model.value}/data`),
        method: 'get',
        params: $http.adornParams({
          'id': dataForm.id
        }),
      }).then(({ data }) => {
        if (data && data.status === 200) {
          dataForm.username = data.data.username
          dataForm.nickname = data.data.nickname
          dataForm.qrcode_url = data.data.qrcode_url
          dataForm.is_bind = data.data.is_bind.value

          useSocket(data.data.qrcode_url, router)
        }
      })
    }
  }

  /**
   * 解除绑定
   */
  const handleCancelBind = (id) => {
    $http({
      url: $http.adornUrl(`/${model.value}/cancel`),
      method: 'post',
      params: $http.adornParams({
        id: id
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        router.go(0)
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    init()
  })
</script>

<style scoped>
  .image {
    max-width: 300px;
    max-height: 300px;
    width: 200px;
    height: 200px;
  }
</style>
